var api = require("../../utils/api");
var app = getApp();

Page({
    data: {
        TabCur: "",
        categoryList: [],
        photos: [],
        nextStart: 0,
        pageIndex:1,
        pageSize:20
    },
    onLoad: function(t) {
        // this.getMenuList(3);
        this.getMenuListNew();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {
        var that = this;
        that.setData({
            pageIndex:1,
            photos: [],
        })
        that.getZoneListNew(that.data.TabCur)
    },
    onReachBottom: function() {
        var that = this;
        that.setData({
            pageIndex:that.data.pageIndex+1,
        })
        that.getZoneListNew(that.data.TabCur)
    },
    tabSelect: function(t) {
        var e = t.currentTarget.dataset.id;
        this.setData({
            TabCur: e,
            nextStart: 0,
            pageIndex:1,
            photos: []
        }),
        this.getZoneListNew(e)
    },
//对接别人的头像
getMenuListNew: function(){
    var that = this
    wx.request({
        url: "https://spare.maibaapp.com/bbs/newUgc/category/list/2",
        method: "GET",
        success(res) {
            console.log(res)
            if (res.data.code == 0) {
                if (res.data.data.categoryList[0]) {
                  var menuList= res.data.data.categoryList;
                    that.setData({
                        categoryList: menuList,
                        TabCur: menuList[0].cid
                    })
                    // that.getPicList(0)
                    that.getZoneListNew(res.data.data.categoryList[0].cid)
                }

            } else {
                wx.showToast({
                    title: res.data.message,
                    icon: 'none'
                })
            }

        }
    })

},



    getMenuList:function(type){
        var that = this
        var params={
            menuType:type
        }
        wx.request({
            url: api.queryMenu,
            method: "POST",
            data: params,
            success(res) {
                console.log(res)
                if(res.data.code==1){
                    that.setData({
                        categoryList:res.data.data,
                        TabCur:res.data.data[0].id
                    })
                    if(res.data.data[0]){
                        that.getZoneList(res.data.data[0].id)
                    }

                }else{
                    wx.showToast({
                        title: res.data.message,
                        icon:'none'
                      })
                }

            }
        })
    },

    info: function(t) {
        var item = t.currentTarget.dataset.item;
        var needcount = app.globalData.downloadPoint;
        wx.navigateTo({
            url: "../photo/photo?item=" + JSON.stringify(item) + "&needcount=" + needcount,
            success: function(t) {},
            fail: function(t) {},
            complete: function(t) {}
        });
    },
    onShareAppMessage: function() {
        return {
            path: "pages/gallery/gallery",
            title: "好友推荐你一个头像壁纸库！"
        };
    },
    getZoneListNew:function(id){
        var that =this
        var pageindex= that.data.nextStart
        var url = "https://spare.maibaapp.com/bbs/newUgc/category/info/2/" + id + "/" + pageindex+ "/" + (pageindex + that.data.pageSize)
        wx.request({
            url: url,
            method: "GET",
            success(res) {
                console.log(res.data)
                var e = {};
                var picList = res.data.data.list;
                console.log(picList.length)
                   if( picList.length < that.data.pageSize ){
                    console.log(picList.length)
                    that.setData({
                        isLastPage: !0,
                        loadtext: "到底啦",
                        showloadmore: !1
                    })
                   }

                        e.photos = that.data.photos.concat(picList),
                        that.setData(e);
                        that.setData({
                            nextStart:res.data.data.nextStart
                        })
                wx.stopPullDownRefresh();
            }
        })
    },
    getZoneList:function(id){
        var that = this
        var params={
            zoneTypeId:id,
            pageIndex:that.data.pageIndex,
            pageCount:that.data.pageSize
        }
        wx.request({
            url: api.queryZoneList,
            method: "POST",
            data: params,
            success(res) {
                console.log(res)
                if(res.data.code==1){
                   var picList = res.data.data;
                   var e = {};
                    picList.length < that.data.pageSize && that.setData({
                        isLastPage: !0,
                        loadtext: "到底啦",
                        showloadmore: !1
                    }),
                    e.photos = that.data.photos.concat(picList),
                    that.setData(e);
                }else{
                    wx.showToast({
                        title: res.data.message,
                        icon:'none'
                      })
                }

            }
        })
    }
});